define(() => {

    function y_axis_stop(sprite) {
        sprite.vy = 0
    }

    function x_axis_stop(sprite) {
        sprite.vx = 0
    }

    return {
        emit_by_keyboard: (player, key, kb) => {
            player.emitting = false
            kb.mount_down(key, () => {
                player.emitting = true
            })
            kb.mount_up(key, () => {
                player.emitting = false
            })
        },
        move_by_keyboard: (player, velocity, kb) => {
            player.vx = 0
            player.vy = 0
            player.facing = 1 // 0 1 2 3: up right down left
            kb.mount_down('w', () => {
                player.vy = -velocity
                player.facing = 0
            })
            kb.mount_up('w', () => { y_axis_stop(player) })
            kb.mount_down('a', () => {
                player.vx = -velocity
                player.facing = 3
            })
            kb.mount_up('a', () => { x_axis_stop(player) })
            kb.mount_down('s', () => {
                player.vy = velocity
                player.facing = 2
            })
            kb.mount_up('s', () => { y_axis_stop(player) })
            kb.mount_down('d', () => {
                player.vx = velocity
                player.facing = 1
            })
            kb.mount_up('d', () => { x_axis_stop(player) })
        },
        ani_by_keyboard: (player, kb) => {
            kb.mount_down('w', () => { player.set_ani('up') })
            // kb.mount_pressing('w', () => { player.set_ani('up') })
            kb.mount_up('w', () => {
                if (player.anime.name == 'up')
                    player.set_ani('up_stop')
            })
            kb.mount_down('a', () => { player.set_ani('left') })
            // kb.mount_pressing('a', () => { player.set_ani('left') })
            kb.mount_up('a', () => {
                if (player.anime.name == 'left')
                    player.set_ani('left_stop')
            })
            kb.mount_down('s', () => { player.set_ani('down') })
            // kb.mount_pressing('s', () => { player.set_ani('down') })
            kb.mount_up('s', () => {
                if (player.anime.name == 'down')
                    player.set_ani('down_stop')
            })
            kb.mount_down('d', () => { player.set_ani('right') })
            // kb.mount_pressing('d', () => { player.set_ani('right') })
            kb.mount_up('d', () => {
                if (player.anime.name == 'right')
                    player.set_ani('right_stop')
            })
        },
        
    }
})